package com.suneee.store.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.orm.GenericDao;
import com.suneee.store.bean.ViewStockRecordDTO;
import com.suneee.store.dao.ViewCompareRecordDetailDao;
import com.suneee.store.entity.ViewCompareRecordDetail;
import com.suneee.store.resources.StaticParam;

@Repository("viewCompareRecordDetailDao")
public class ViewCompareRecordDetailDaoImpl extends GenericDao<ViewCompareRecordDetail, String>implements ViewCompareRecordDetailDao {

	@Override
	public Page<ViewCompareRecordDetail> getPageByRecordId(ViewStockRecordDTO dto) {
		// TODO Auto-generated method stub
		String sql = "select * from v_compare_record_detail where record_id = ? and (status = ? or status = ?) ";
		Object[] args = new Object[3];
		args[0] = dto.getRecordId();
		args[1] = StaticParam.SYNC_STATUS_WAITTING;
		args[2] = StaticParam.SYNC_STATUS_FAIL;
		return this.executeQuery(sql, dto, args);
	}

	@Override
	public ViewCompareRecordDetail getRecordByRecordIdAndSkuId(String recordId, Integer skuId){
		String sql = "select * from v_compare_record_detail where record_id = ? and sku_id = ?";
		Object[] args = new Object[2];
		args[0] = recordId;
		args[1] = skuId;
		List<ViewCompareRecordDetail> list = this.executeQuery(sql, args);
		if(list == null || list.isEmpty())
			return null;
		return list.get(0);
	}

	@Override
	public long getTotalWaitingByRecordId(ViewStockRecordDTO dto) {
		// TODO Auto-generated method stub
		String sql = "select count(*) as total from v_compare_record_detail where record_id = ? and status = ? ";
		Object[] args = new Object[2];
		args[0] = dto.getRecordId();
		args[1] = StaticParam.SYNC_STATUS_WAITTING;
		List<Map<String, Object>> list = this.queryForList(sql, args);
		if(list == null || list.isEmpty())
			return 0;
		return (long)list.get(0).get("total");
	}

	@Override
	public long getTotalFailByRecordId(ViewStockRecordDTO dto) {
		// TODO Auto-generated method stub
		String sql = "select count(*) as total from v_compare_record_detail where record_id = ? and status = ? ";
		Object[] args = new Object[2];
		args[0] = dto.getRecordId();
		args[1] = StaticParam.SYNC_STATUS_FAIL;
		List<Map<String, Object>> list = this.queryForList(sql, args);
		if(list == null || list.isEmpty())
			return 0;
		return (long)list.get(0).get("total");
	}

	@Override
	public List<ViewCompareRecordDetail> getListByRecordId(ViewStockRecordDTO dto) {
		// TODO Auto-generated method stub
//		String sql = "select * from v_compare_record_detail where record_id = ? and (status = ? or status = ?) ";
		String sql = "record_id = ? and (status = ? or status = ?) ";
		Object[] args = new Object[3];
		args[0] = dto.getRecordId();
		args[1] = StaticParam.SYNC_STATUS_WAITTING;
		args[2] = StaticParam.SYNC_STATUS_FAIL;
		return this.selectWhere(sql, args);
	}
	
}
